package com.dailywork.basepro.thread.objectLock;

/**
 * ҵ�����ݵ��������(��������ͬ�����첽֪ʶ����) Ҫ��ȡ��������Ҫ��һ����,����ǰ�Ͷ���
 * �������,Ҫ��֤������һ����,��Ӷ�д������
 *
 * @author lhy
 * @time 2017.12.22
 */
public class DirtyRead {

    private String username = "haige";
    private String password = "123";

    public synchronized void setValue(String username, String password) {
        this.username = username;

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        this.password = password;
        System.out.println("setValue���ս��: username = " + username + ", password = " + password);
    }

    // ��ʼʱ����synchronized,Ϊ�˷�ӳ�������,ȥ���÷�����synchronized
    public void getValue() {
        System.out.println(
                "getValue�����õ�: username = " + this.username + ", password = " + this.password);
    }

    public static void main(String[] args) throws Exception {
        final DirtyRead dr = new DirtyRead();
        Thread t1 =
                new Thread(
                        new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                dr.setValue("lhy", "456");
                            }
                        });
        t1.start();
        Thread.sleep(1000);
        dr.getValue();
    }
}
